package custom.base.http;

import android.content.Context;
import android.text.TextUtils;

import custom.base.utils.Md5Utils;
import custom.base.utils.TxtUtil;
import custom.base.utils.store.ConstantsPreference;
import custom.base.utils.store.PreferencesManager;


/**
 * Created by cubebox on 2017/7/25.
 */

public class SignatureUtils {
    public static String APP_ID = "mobileapp";

//        public static String APPSECRET = "VGxjNWRGUkVWbmRsVkZwYVYwVktNdz09";//测试环境
    public static String APPSECRET = "ktNdz0VkZGUkVWbmRYVYGxjNWRsVwVw9";//发布环境
    /**
     * 缓存秘钥，避免每次都从文件当中去取
     */
    public static String cacheSecret = "";


    public static void clearCacheSecret() {
        SignatureUtils.cacheSecret = "";
    }

    /**
     * 得到签名秘钥,用户动态修改签名秘钥时使用
     */
    public static String getSignatureCache(Context context) {
        String ip;
        if (TxtUtil.isEmpty(cacheSecret)) {
            String tmp = PreferencesManager.getInstance(context).getString(ConstantsPreference.PRE_SIGNATURE, "");
            if (!TextUtils.isEmpty(tmp)) {
                ip = tmp;
            } else {
                ip = APPSECRET;
            }
        } else {
            ip = cacheSecret;
        }

        return ip;
    }

    public static void setSignatureCache(Context context, String signature) {
        if (!TextUtils.isEmpty(signature))
            PreferencesManager.getInstance(context).putString(ConstantsPreference.PRE_SIGNATURE, signature);
    }

    /**
     * 得到签名
     */
    public static String getSignature(Context context, long timeStamp) {

        String res = APP_ID + getSignatureCache(context) + timeStamp + TokenManager.getInstance(context).getToken();
        res = Md5Utils.MD5Encode(res);
        return res;
    }

    /**
     * 得到登录的签名不带token
     */
    public static String getLoginSignature(Context context, long timeStamp) {
        String res = APP_ID + getSignatureCache(context) + timeStamp;
        res = Md5Utils.MD5Encode(res);
        return res;
    }

    /**
     * 得到签名
     */
    public static String getSignature(Context context) {

        String res = APP_ID + getSignatureCache(context) + (getTimeStampLong()) + TokenManager.getInstance(context).getToken();
        res = Md5Utils.MD5Encode(res);
        return res;
    }

    /**
     * 得到登录的签名不带token
     */
    public static String getLoginSignature(Context context) {
        String res = APP_ID + getSignatureCache(context) + (getTimeStampLong());
        res = Md5Utils.MD5Encode(res);
        return res;
    }

    /**
     * 当前静态时间戳，单位毫秒
     */
    private static long currentTimeMillis = 0;
    /**
     * 替换标记，默认2秒，当调用一次接口该值会递加，在未调用超过限定时间后才能刷新 currentTimeMillis 值
     */
    private static long replaceTime = 1000;

    /**
     * 得到时间戳
     */
    public static String getTimeStamp() {
        return getTimeStampLong() + "";
    }

    /**
     * 得到时间戳,在同一秒内取出的值不变
     */
    public static long getTimeStampLong() {
        long tmp = System.currentTimeMillis();
        if (tmp - currentTimeMillis >= replaceTime) {//间隔时间大于2秒才刷新时间
            currentTimeMillis = tmp;
            replaceTime = 1000;
        } else {
            replaceTime += 50;
        }
        return currentTimeMillis / 1000;
    }
}
